<?php
class Application_Model_DbTable_Link extends CF_DbTable_Table
{
	protected $_name = 'links';
	protected $_primary = 'id';
	protected $_columns = array( 'title'=>'',
								 'category_id'=>null,
								 'url'=>''

								);

	/**
	 * 
	 * getLinks
	 * @param unknown_type $id
	 * @param unknown_type $limit
	 * return link, url, category, category_id, id
	 * join categories
	 * return rowset or row(if $id is set)
	 */
	public function getLinks($id = null, $limit = null)
	{
		$select = $this->getAdapter()->select()
		->from(array('link'=>$this->_name),
		array('title'=>'link.title','link.id','category_id','url'))
		->joinLeft(array('cat'=>'categories'),
				'link.category_id=cat.id',
		array('category'=>'cat.title'))
		->order(array('category','title'));

		if(!is_null($id))
		{
//			var_dump($id);
//			exit;
			$select->where('link.id=?',(int)$id);
			$resource = $this->getAdapter()->query($select);
			$result = $resource->fetchAll();
			return $result[0];
		}

		if(!is_null($limit))
		{
			$select->limit($limit);
		}
		$resource = $this->getAdapter()->query($select);
		$result = $resource->fetchAll();

		return $result;
	}

}